#!/usr/bin/env python
#coding: iso-8859-1

"""Documentation for world.py

"""
#Default Imports
import sys

from pandac.PandaModules import loadPrcFileData
loadPrcFileData("", "audio-cache-limit 128") #Audio Cache Limit
loadPrcFileData("", "win-size 720 516") #Audio Cache Limit
loadPrcFileData("", "window-title " + "deBlock2")
loadPrcFileData("", "sync-video 1")
loadPrcFileData('', 'show-frame-rate-meter 1')
#loadPrcFileData('', 'support-threads #f') 
#loadPrcFileData('', 'loader-num-threads 64')
#loadPrcFileData('', 'allow-live-flatten #t')
loadPrcFileData('', 'allow-incomplete-render 1')
#loadPrcFileData("", "want-directtools 1")
#loadPrcFileData("", "want-tk 1")


#Panda specific imports for this module
from pandac.PandaModules import *
from direct.showbase import DirectObject 
from direct.gui.DirectGui import OnscreenText 
from direct.filter.CommonFilters import CommonFilters
from xml.sax import make_parser
from direct.showbase.ShowBase import ShowBase
from pandac.PandaModules import CollisionTraverser, CollisionHandlerPusher
from panda3d.core import Vec3
from panda3d.ai import *
from midi import *
from menuClass import Menu

from input.KeyboardMouseHandler import KeyboardMouseHandler
import camera
import globals
import xmlr

#User imports
import globals
import player
import enemyMidi
import friendMidi
import enemy
import tunnel
import keyb
import enemyBoss
import midi_input
import midifile


class World(DirectObject.DirectObject):
    """Class world.
    

    """
    def __init__(self):
        base.setBackgroundColor(0, 0, 0)
        base.disableMouse() 
        props = WindowProperties()
        props.setCursorHidden(True)
        base.enableParticles()
        base.win.requestProperties(props)

        self.filters = CommonFilters(base.win, base.cam)
        self.filters.setBloom(blend=(0.05,0.05,0.05,1), desat=-0.125, intensity=9.0, size="large")

        globals.PRELOADER = xmlr.Preloader(globals.LEVEL_1_DIR)
        globals.PRELOADER.step()

        #taskMgr.popupControls()
	cam = camera.Camera()
        #render.setTwoSided(True)
        render.setShaderAuto()
        PStatClient.connect()
        #Set camera lens Far distance
        #base.camLens.setFov(40)
        base.camLens.setNear(1)
        base.camLens.setFar(5000)
        base.camLens.setFocalLength(0.5)
        #base.camLens.setAspectRatio(800.0 / 300.0)
        #base.camLens.setFilmSize(28, 40)
        #Initialize traverser
        
        base.cTrav = CollisionTraverser()
        base.cTrav.setRespectPrevTransform(1)
        globals.PhysicsBox = CollisionHandlerEvent()
        globals.PhysicsBox.addInPattern('into-%in')
        #globals.PhysicsBox.addOutPattern('outof-%in')
        globals.WORLD = self

        globals.PRELOADER.step()

        #Initializes the XML parser and loads level1
        handler = xmlr.RezHandler(globals.LEVEL_1_DIR)
        parser = make_parser()
        parser.setContentHandler(handler)
        parser.parse(open(globals.LEVEL_1_XML))  
        taskMgr.add(self.CollisionCheck,"CollisionCheck")

        globals.KEYBOARD = KeyboardMouseHandler()
        self.createStartMenu()
        globals.KEYBOARD_DEBUG = keyb.Keyboard()
        globals.STAGE.init(0)

    def initi(self):
        print "ubuububtu"
        globals.PLAYER.init()
        globals.STAGE.startLevel()

    def createStartMenu(self):
        menu = Menu(None,None,None)

        menu.initMenu([0,None,
			["New Game", "Options", "Quit"],
			[[self.initi], [self.createReadyDialogue],
				[base.userExit]],
			[[None],[None],[None]]])

    def createReadyDialogue(self):
        menu = Menu(None,None,None)

        menu.initMenu([0,None,
			["Back", "Quit"],
			[[self.createStartMenu], [base.userExit]],
			[[None],[None]]])

    def run(self):
        """Manages and runs everything."""
        
        run()


    def CollisionCheck(self,task):
        base.cTrav.traverse(render)
        return task.again

    def tupendo(self):
        print "tupendo"